package cn.com.generaldata.os.ws.impl;

import java.util.ArrayList;
import java.util.List;

import cn.com.generaldata.os.constant.FilterConstant;
import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.paging.FilterRule;
import cn.com.generaldata.os.service.common.RoleService;
import cn.com.generaldata.os.vo.RoleVO;
import cn.com.generaldata.os.ws.intf.RoleWSInterface;

public class RoleWS implements RoleWSInterface {

	private RoleService roleService;

	@Override
	public RoleVO queryRoleById(String id) {
		return toVo(roleService.queryRoleById(id));
	}

	@Override
	public List<RoleVO> queryRoles() {
		Filter filter = new Filter();
		filter.setOrder(FilterConstant.ASC);
		filter.setSortBy("roleName");

		filter.addRule(new FilterRule(FilterConstant.NOT_EQUAL, "roleDesc", "Root"));
		return toVoList(roleService.queryRoles(filter, null));
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	private RoleVO toVo(cn.com.generaldata.os.pojo.Role pRole) {
		if (pRole != null) {
			return new RoleVO(pRole.getId(), pRole.getRoleName(), pRole.getRoleDesc());
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	private List<RoleVO> toVoList(cn.com.generaldata.os.paging.Result pResult) {
		List<RoleVO> roles = new ArrayList<RoleVO>();
		for (cn.com.generaldata.os.pojo.Role pRole : (List<cn.com.generaldata.os.pojo.Role>) pResult
				.getOperationResult()) {
			roles.add(toVo(pRole));
		}
		return roles;
	}

}
